package com.baidu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baidu.entity.PageResult;
import com.baidu.mapper.MemberMapper;
import com.baidu.pojo.Member;
import com.baidu.service.MemberService;
import com.baidu.utils.MD5Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberMapper memberMapper;

	@Override
	public Member findByTelephone(String telephone) {
		return memberMapper.findByTelephone(telephone);
	}

	@Override
	public void register(Member member) {
//		为了添加方法的通用性，对会员的密码进行加密
		if (member.getPassword()!=null&&member.getPassword().length()>0){
			String passwordM = MD5Utils.md5(member.getPassword());
	member.setPassword(passwordM);

		}

		memberMapper.add(member);
	}

	/**
	 * 根据id查询用户
	 * @param id
	 * @return
	 */
	@Override
	public Member findMemberById(Integer id) {
		return memberMapper.findMemberById(id);
	}

	@Override
	public ArrayList<Integer> findMemberByMonth(ArrayList<String> listM) {

		ArrayList<Integer> list = new ArrayList<Integer>();
		for (String s : listM) { //这里的会员是累计增加的，并不是统计每一个月的人数。

			s=s+".31";
		int i=	memberMapper.findMemberCountByMonthBefor(s);
		list.add(i);

		}


		return list;
	}

	/**
	 * 根据页码条件查找数据分页
	 * @param currentPage
	 * @param pageSize
	 * @param queryString
	 * @return
	 */
	@Override
	public PageResult findByPage(Integer currentPage, Integer pageSize, String queryString) {
		PageHelper.startPage(currentPage,pageSize);
		Page<Member> page=memberMapper.findByPage(queryString);
		return new PageResult(page.getTotal(),page.getResult());
	}

	/**
	 * 添加会员
	 * @param member
	 */
	@Override
	public void addMember(Member member) {
		memberMapper.addMember(member);
	}

	/**
	 * 编辑会员
	 * @param member
	 */
	@Override
	public void editMember(Member member) {
		memberMapper.editMember(member);
	}

	/**
	 * 通过ID删除会员
	 * @param id
	 */
	@Override
	public void deleteMemberById(Integer id) {
		memberMapper.deleteMember(id);
	}


}
